<template>
  <div class="editor-header">
    <div class="left">
    </div>

    <div class="middle">
      <div class="btn">
        <div class="group-menu-item">
          <div class="menu-item" @click="setDialogForExport('pptx')">
            <IconDownload class="icon" />
          </div>
        </div>
        <span class="btn-title">{{ $t('editorHeader.export') }}</span>
      </div>
      <div class="btn">
        <div class="group-menu-item">
          <div class="menu-item" @click="enterScreening()">
            <IconPpt class="icon" />
          </div>
          <Popover trigger="click" center>
            <template #content>
              <PopoverMenuItem @click="enterScreeningFromStart()">{{ $t('editorHeader.screeningFromBegin') }}</PopoverMenuItem>
              <PopoverMenuItem @click="enterScreening()">{{ $t('editorHeader.screeningFromCurrent') }}</PopoverMenuItem>
            </template>
            <div class="arrow-btn"><IconDown class="arrow" /></div>
          </Popover>
        </div>
        <span class="btn-title">{{ $t('editorHeader.screening') }}</span>
      </div>
      <Popover trigger="click" placement="bottom-start" v-model:value="mainMenuVisible">
        <template #content>
          <FileInput accept=".pptist"  @change="files => {
            importSpecificFile(files)
            mainMenuVisible = false
          }">
            <PopoverMenuItem>
              <i18next :translation="$t('editorHeader.importFile')">
                <template v-slot:fileType>
                  <span>pptist</span>
                </template>
              </i18next>
            </PopoverMenuItem>
          </FileInput>
          <FileInput accept="application/vnd.openxmlformats-officedocument.presentationml.presentation"  @change="files => {
            importPPTXFile(files)
            mainMenuVisible = false
          }">
            <PopoverMenuItem>
              <i18next :translation="$t('editorHeader.importFile')">
                <template v-slot:fileType>
                  <span>pptx</span>
                </template>
              </i18next>  
            </PopoverMenuItem>
          </FileInput>
          <PopoverMenuItem @click="setDialogForExport('pptx')">{{ $t('editorHeader.exportFile') }}</PopoverMenuItem>
          <PopoverMenuItem @click="resetSlides(); mainMenuVisible = false">{{ $t('editorHeader.resetSlide') }}</PopoverMenuItem>
          <!-- <PopoverMenuItem @click="goLink('https://github.com/pipipi-pikachu/PPTist/issues')">意见反馈</PopoverMenuItem> -->
          <!-- <PopoverMenuItem @click="goLink('https://github.com/pipipi-pikachu/PPTist/blob/master/doc/Q&A.md')">常见问题</PopoverMenuItem> -->
          <PopoverMenuItem>{{ $t('editorHeader.feedback') }}</PopoverMenuItem>
          <PopoverMenuItem>{{ $t('editorHeader.qa') }}</PopoverMenuItem>
          <PopoverMenuItem @click="mainMenuVisible = false; hotkeyDrawerVisible = true">{{ $t('editorHeader.shortcutKey') }}</PopoverMenuItem>
        </template>
        <div class="btn">
          <div class="group-menu-item">
            <div class="menu-item">
              <IconHamburgerButton class="icon" />
            </div>
          </div>
          <span class="btn-title">{{ $t('editorHeader.more') }}</span>
        </div>
      </Popover>

    </div>

    <div class="right">
      <!-- <div class="menu-item" v-tooltip="'历史版本'">
        <IconHistory class="icon" />
      </div>
      <div class="menu-item">
        <IconUser class="icon" />
      </div> -->
      <!-- 国际化切换菜单 -->
      <!-- <div class="menu-item">
        <International class="icon" />
      </div> -->
    </div>

    <Drawer
      :width="320"
      v-model:visible="hotkeyDrawerVisible"
      placement="right"
    >
      <HotkeyDoc />
    </Drawer>
    
    <FullscreenSpin :loading="exporting" :tip="$t('editorHeader.importing')" />
  </div>
</template>

<script lang="ts" setup>
import { updateDoc, updateTemplate } from '@/api'
import Drawer from '@/components/Drawer.vue'
import FileInput from '@/components/FileInput.vue'
import FullscreenSpin from '@/components/FullscreenSpin.vue'
import Popover from '@/components/Popover.vue'
import PopoverMenuItem from '@/components/PopoverMenuItem.vue'
import useImport from '@/hooks/useImport'
import useScreening from '@/hooks/useScreening'
import useSlideHandler from '@/hooks/useSlideHandler'
import useStatusNotifier from '@/hooks/useStatusNotifier'
import { useMainStore, useSlidesStore, useWikiStore } from '@/store'
import type { DialogForExportTypes } from '@/types/export'
import { IframeViewMode } from '@/types/iframeViewMode'
import { storeToRefs } from 'pinia'
import { ref, watch } from 'vue'
import HotkeyDoc from './HotkeyDoc.vue'

const mainStore = useMainStore()
const slidesStore = useSlidesStore()
const { enterScreening, enterScreeningFromStart } = useScreening()
const { importSpecificFile, importPPTXFile, exporting } = useImport()
const { resetSlides } = useSlideHandler()
const { sendSavingMsg, sendSavedMsg } = useStatusNotifier()
const wikiStore = useWikiStore()
const { documentId, iframeViewMode} = storeToRefs(wikiStore)

const mainMenuVisible = ref(false)
const hotkeyDrawerVisible = ref(false)

const savePPT = () => {
  const {title, slides, theme} = useSlidesStore()
  if (iframeViewMode.value === IframeViewMode.STANDARD_TEMPLATE) { // 保存模板
    updateTemplate({
      id: documentId.value,
      content: JSON.stringify({title, slides, theme}),
    }).then(res => {
      sendSavedMsg(res?.data?.date ?? new Date())
    }).catch(err => {
      console.error(err)
    })
  }
  else if (iframeViewMode.value === IframeViewMode.STANDARD) {
    // 保存文档
    updateDoc({
      id: documentId.value,
      content: JSON.stringify({title, slides, theme}),
      deleteStatus: 0
    }).then(res => {
      sendSavedMsg(res?.data?.date ?? new Date())
    }).catch(err => {
      console.error(err)
    })
  }
  
}

let count:number = 0
let timer:any
watch(slidesStore, (newValue) => {
  if (count !== 0) {
    sendSavingMsg()
    clearTimeout(timer)
    timer = setTimeout(() => savePPT(), 1000)
  } 
  else {
    count++
  }
})

const setDialogForExport = (type: DialogForExportTypes) => {
  mainStore.setDialogForExport(type)
  mainMenuVisible.value = false
}
</script>

<style lang="scss" scoped>
.editor-header {
  height: 50px;
  overflow: hidden;
  background-color: #fff;
  user-select: none;
  border-bottom: 1px solid $borderColor;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.left {
  height: 40px;
  display: flex;
  justify-content: start;
  align-items: center;

  .menu-item{
    height: 40px;
  }
}
.middle{
  height: 50px;
  display: flex;
  justify-content: center;
  align-items: center;

  .btn{
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 6px;
    transform: translateX(-45px);
    cursor: pointer;

    .btn-title{
      white-space:nowrap;
      font-size: 12px;
    }

    &:hover{
      background-color: #f1f1f1;
    }
  }
}
.right {
  height: 40px;
  display: flex;
  justify-content: end;
  align-items: center;

  .menu-item{
    height: 40px;
    padding: 0 11px;

    .icon{
      size: 18px;
      color: #666;
    }
  }
}
.menu-item {
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  padding: 0 10px;
  border-radius: $borderRadius;
  cursor: pointer;

  .icon {
    font-size: 18px;
    color: #666;
  }

  &:hover {
    background-color: #f1f1f1;
  }
}
.group-menu-item {
  height: 24px;
  display: flex;
  margin: 0 8px;
  padding: 0 2px;
  border-radius: $borderRadius;

  &:hover {
    background-color: #f1f1f1;
  }

  .menu-item {
    padding: 0 3px;
  }
  .arrow-btn {
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
  }
}
.title {
  height: 32px;
  margin-left: 2px;
  font-size: 13px;

  .title-input {
    width: 200px;
    height: 100%;
    padding-left: 0;
    padding-right: 0;
  }
  .title-text {
    min-width: 20px;
    max-width: 200px;
    line-height: 32px;
    padding: 0 6px;
    border-radius: $borderRadius;
    cursor: pointer;

    @include ellipsis-oneline();

    &:hover {
      background-color: #f1f1f1;
    }
  }
}
.github-link {
  display: inline-block;
  height: 30px;
}
</style>@/configs/saveStateTypes@/hooks/useStatusNotify